package cn.com.darly.videoandroidpm.ui;

import android.os.Bundle;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.SpannedString;
import android.text.style.AbsoluteSizeSpan;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.LinearLayout;

import com.darly.common.ToastApp;
import com.darly.dview.framework.ContentBinder;
import com.darly.dview.framework.ViewsBinder;
import com.darly.dview.widget.clearedit.ClearEditText;
import com.darly.dview.widget.header.TitleView;

import cn.com.darly.videoandroidpm.R;
import cn.com.darly.videoandroidpm.base.BaseActivity;
import cn.com.darly.videoandroidpm.ui.biz.RegisterListener.RegisterInterface;
import cn.com.darly.videoandroidpm.ui.presenter.RegisterPresenter;

/** 用户注册页面
 * @author Darly/张宇辉/2018/4/25 15:09
 * @version 1.0/cn.com.darly.videoandroidpm.ui
 */
@ContentBinder(R.layout.activity_register)
public class RegisterActivity extends BaseActivity implements OnClickListener,RegisterInterface{
    @ViewsBinder(R.id.id_base_title)
    public TitleView id_base_title;
    @ViewsBinder(R.id.id_register_anim)
    LinearLayout id_register_anim;

    @ViewsBinder(R.id.id_register_tel)
    ClearEditText id_register_tel;
    @ViewsBinder(R.id.id_register_btn)
    Button id_register_btn;
    @ViewsBinder(R.id.id_register_code)
    ClearEditText id_register_code;

    private String ServerCode;
    RegisterPresenter presenter;

    @Override
    protected void initView(Bundle savedInstanceState) {
        //添加登录界面动画效果
        id_register_anim.setAnimation(AnimationUtils.loadAnimation(this,R.anim.login_up));
        int res =  getIntent().getIntExtra("title",0);
        if (res == 0) {
            id_base_title.setTitle(R.string.app_name);
        }else {
            id_base_title.setTitle(res);
        }
        id_base_title.removeBackground(R.drawable.ic_title_background);
        presenter = new RegisterPresenter(this);
    }

    @Override
    protected void loadData() {
        SpannableString nams = new SpannableString("请输入手机号码");//定义hint的值
        AbsoluteSizeSpan namass = new AbsoluteSizeSpan(16, true);//设置字体大小 true表示单位是sp
        nams.setSpan(namass, 0, nams.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        id_register_tel.setHint(new SpannedString(nams));
        SpannableString codes = new SpannableString("请输入验证码");//定义hint的值
        AbsoluteSizeSpan codeser = new AbsoluteSizeSpan(16, true);//设置字体大小 true表示单位是sp
        nams.setSpan(codeser, 0, codes.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        id_register_code.setHint(new SpannedString(codes));
    }

    @Override
    protected void initListener() {
        id_base_title.setLeftBackOneListener(R.mipmap.ic_title_back,this);
        id_base_title.setRightViewRightTextOneListener("下一步",this);
        id_register_btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        presenter.onClickDown(this,v);
    }

    @Override
    public void onCheckSuccess(String code) {
        ServerCode = code;
        id_register_code.setText(code);
    }

    @Override
    public void onCheckFailed(String info) {
        ToastApp.showToast(info);
    }

    @Override
    public boolean isCodeRight(){
        if (id_register_code.getText().toString().trim().equals(ServerCode)){
            return true;
        }else {
            return false;
        }
    }

    @Override
    public String getTel() {
        return id_register_tel.getText().toString().trim();
    }


    @Override
    public void end() {
        onBackPressed();
    }

    @Override
    public void setDisableClick() {

    }

    @Override
    public void setEnableClick() {

    }
}
